package com.base.chat.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.chat.R;
import com.base.chat.bean.MyFriendListBean;
import com.base.commonlib.widget.OvalImageView;

import java.util.List;

/**
 * Created by zw on 2020/2/27.
 */

public class CreateGroupForFriendListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static int HEAD_COUNT = 1;
    private final static int FOOT_COUNT = 0;

    private final static int TYPE_HEAD = 0;
    private final static int TYPE_CONTENT = 1;
    private final static int TYPE_FOOTER = 2;
    private Context context;
    private Activity activity;
    private List<MyFriendListBean> list;

    public CreateGroupForFriendListAdapter(Context context, Activity activity, List<MyFriendListBean> list) {
        this.context = context;
        this.list =  list;
        this.activity =  activity;
    }

    public int getContentSize(){
        return list.size();
    }

    public boolean isHead(int position){
        return HEAD_COUNT != 0 && position == 0;
    }

    public boolean isFoot(int position){
        return FOOT_COUNT != 0 && position == getContentSize() + HEAD_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        int contentSize = getContentSize();
        if (HEAD_COUNT != 0 && position == 0){ // 头部
            return TYPE_HEAD;
        }else if(FOOT_COUNT != 0 && position == HEAD_COUNT + contentSize){ // 尾部
            return TYPE_FOOTER;
        }else{
            return TYPE_CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEAD){
            View itemView = LayoutInflater.from(context).inflate(R.layout.header_create_group_chat,parent,false);
            return new CreateGroupForFriendListAdapter.HeadHolder(itemView);
        }else if(viewType == TYPE_CONTENT){
            View itemView = LayoutInflater.from(context).inflate(R.layout.item_create_group_for_friends,parent,false);
            return new CreateGroupForFriendListAdapter.ContentHolder(itemView);
        }
        else{
            View itemView = LayoutInflater.from(context).inflate(R.layout.header_create_group_chat,parent,false);
            return new CreateGroupForFriendListAdapter.FootHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof CreateGroupForFriendListAdapter.HeadHolder){ // 头部
            CreateGroupForFriendListAdapter.HeadHolder headHolder = (CreateGroupForFriendListAdapter.HeadHolder) holder;
            headHolder.search.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }else if(holder instanceof CreateGroupForFriendListAdapter.ContentHolder){ // 内容
            CreateGroupForFriendListAdapter.ContentHolder myHolder = (CreateGroupForFriendListAdapter.ContentHolder) holder;

//            myHolder.name.setText(list.get(position - 1).getName());
//            myHolder.address.setText(list.get(position - 1).getAddress2());
//            myHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    LocationCheckedEntity locationCheckedEntity=new LocationCheckedEntity();
//                    locationCheckedEntity.setAddress(list.get(position-1).getName());
//                    locationCheckedEntity.setLatitude(list.get(position-1).getLatitude());
//                    locationCheckedEntity.setLonTitude(list.get(position-1).getLonTitude());
//                    locationCheckedEntity.setState(0x00000101);
//                    RxBus2.getInstance().post(locationCheckedEntity);
//                    activity.finish();
//                }
//            });
        }else{ // 尾部

        }
    }

    @Override
    public int getItemCount() {
        return 10 + HEAD_COUNT + FOOT_COUNT;
    }

    // 头部
    private class HeadHolder extends RecyclerView.ViewHolder{
        EditText et_group_name;
        EditText search;
        OvalImageView ov_group_img;

        public HeadHolder(View itemView) {
            super(itemView);
            et_group_name = (EditText) itemView.findViewById(R.id.et_group_name);
            search = (EditText) itemView.findViewById(R.id.search);
            ov_group_img = (OvalImageView) itemView.findViewById(R.id.ov_group_img);
        }
    }

    // 内容
    private class ContentHolder extends RecyclerView.ViewHolder{
        CheckBox it_is_check;
        TextView it_user_name;
        OvalImageView it_user_img;
        public ContentHolder(View itemView) {
            super(itemView);
            it_is_check = (CheckBox) itemView.findViewById(R.id.it_is_check);
            it_user_name = (TextView) itemView.findViewById(R.id.it_user_name);
            it_user_img = (OvalImageView) itemView.findViewById(R.id.it_user_img);
        }
    }

    // 尾部
    private class FootHolder extends RecyclerView.ViewHolder{
        public FootHolder(View itemView) {
            super(itemView);
        }
    }
}
